package com.activities;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bean.TopPager;
import com.bean.TopicMap;
import com.example.lizhiinformation.R;
import com.myadapter.FrontPage_Topadapter;
import com.myadapter.TopicAdapter;
import com.myadapter.TopicMapAdapter;
import com.ownload.Httputil;
import com.ownload.Httputil.OnNetWorkRespones;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class MapInformation extends Activity {
	private ArrayList<TopicMap> list;
	private ViewPager mappager;
	private TextView maptitle;
	private TextView contants;
	private TextView pagecounts;
	private  String title=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_information);
		String url = getIntent().getExtras().getString("url");
		 mappager = (ViewPager) findViewById(R.id.topic_viewpage);
		 maptitle = (TextView) findViewById(R.id.maptitle);
		 contants = (TextView) findViewById(R.id.contants);
		 pagecounts = (TextView) findViewById(R.id.pagecounts);
		
		Httputil.OnWorkDown("http://litchiapi.jstv.com/api/GetMap?id=" + url
				+ "&val=CC065D82FE0D017649BC60040E7D577A====",
				new OnNetWorkRespones() {				

					@Override
					public void OK(String respose) {
						try {
							JSONObject object = new JSONObject(respose);
							JSONObject mapObject = object.getJSONObject("paramz")
							.getJSONObject("map");
							title=mapObject.getString("subject");
							JSONArray pics1 = mapObject.getJSONArray("pics");
							list = new ArrayList<TopicMap>();
							for (int i = 0; i < pics1.length(); i++) {
								JSONObject picsobj = pics1.getJSONObject(i);
								TopicMap map = new TopicMap();
								map.setId(picsobj.getString("id"));
								map.setPhoto(picsobj.getString("photo"));
								map.setRaw(picsobj.getString("raw"));
								map.setSubject(picsobj.getString("subject"));
								list.add(map);
							}
							TopicMapAdapter adapter = new TopicMapAdapter(list, MapInformation.this);
							contants.setText(list.get(0).getSubject());
							pagecounts.setText("1"+"/"+list.size());
							maptitle.setText(title);
							mappager.setAdapter(adapter);
							mappager.setOnPageChangeListener(new OnPageChangeListener() {
								@Override
								public void onPageSelected(int arg0) {
									contants.setText(list.get(arg0).getSubject());
									pagecounts.setText((arg0+1)+"/"+list.size());
									maptitle.setText(title);
									if(arg0==list.size()-1){
										Toast.makeText(MapInformation.this, "这是最后一张", 0).show();
									}
								}
								@Override
								public void onPageScrolled(int arg0, float arg1, int arg2) {
								}
								
								@Override
								public void onPageScrollStateChanged(int arg0) {
								}
							});
							
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void ERROR(String error) {
						// TODO Auto-generated method stub
					}
				});
		
	}

}
